package com.cetc;

import org.junit.Test;

import java.io.*;

/**
 * @Classname IOReaderWriterTest
 * @Description 字符输入输出流测试
 * @Date 2022/11/24 11:08
 * @Created by louzhuangxi
 */
public class IOReaderWriterTest {
    /**
     * 将hello.txt中的内容读入到内存
     */
    @Test
    public void test01() {
        File file = new File("data/hello.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            int len;
            while ((len = fr.read()) != -1) {
                System.out.println((char) len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 将hello.txt中的内容读入到内存方式二
     */
    @Test
    public void test02() {
        File file = new File("data/hello.txt");
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf)) != -1) {
                String data = new String(cbuf, 0, len);
                System.out.println(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试FileWriter的基本用法
     * 如果文件不存在,则新创建文件并写入内容,如果构造器中的参数为true，则表示文件追加
     */
    @Test
    public void test03() {
        File file = new File("data/hello1.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(file, true);
            fw.write("I have a dream");
            fw.write("\n");
            fw.write("you need to have a dream");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试FileWriter和FileReader的基本使用
     */
    @Test
    public void test04() {
        File sourceFile = new File("data/hello.txt");
        File targetFile = new File("data/hello2.txt");
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(sourceFile);
            fw = new FileWriter(targetFile);
            int len;
            char[] cBuf = new char[5];
            while ((len = fr.read(cBuf)) != -1) {
                fw.write(cBuf, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
